home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / DropShell / DSAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-09  |  8.4 KB  |  277 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.c
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **    MTC            Marshall Clow
  20. **    SCS            Stephan Somogyi
  21. **
  22. *******************************************************************************
  23. **                      R E V I S I O N   H I S T O R Y
  24. *******************************************************************************
  25. **
  26. **      Date        Author    Description
  27. **    ---------    ------    ---------------------------------------------
  28. **    20 Feb 94    LDR        Modified _HandleDocs to pass item count to preflight & postflight
  29. **    11 Dec 93    SCS        Universal Headers/UPPs (Phoenix 68k/PPC & PPCC)
  30. **                        Skipped System 6 compatible rev of DropShell source
  31. **    11/24/91    LDR        Added a handler for 'pdoc' as per DTS recommendation
  32. **                            This caused some reorg & userProc routine changes
  33. **                            I also created a new common AEVT doc extractor
  34. **                        Cleaned up error handling by adding FailErr
  35. **                        Cleaned up the placement of braces
  36. **                        Added the passing of a userDataHandle to the odoc/pdoc routines
  37. **    10/29/91    SCS        Changes for THINK C 5
  38. **    10/28/91    LDR        Officially renamed DropShell (from QuickShell)
  39. **                        Added a bunch of comments for clarification
  40. **    10/06/91    MTC        Converted to MPW C
  41. **    04/09/91    LDR        Added to Projector
  42. **
  43. ******************************************************************************/
  44.  
  45. #include <Windows.h>
  46.  
  47. #include "DSGlobals.h"
  48. #include "DSUserProcs.h"
  49. #include "DSAppleEvents.h"
  50.  
  51.  
  52. AEEventHandlerUPP    OAPPHandlerUPP, ODOCHandlerUPP, PDOCHandlerUPP, QUITHandlerUPP;
  53.  
  54. /*
  55.     This routine does all initialization for AEM, including the
  56.     creation and then population of the dispatch table.
  57. */
  58. #pragma segment Initialize
  59. pascal void InitAEVTStuff ( void )
  60. {
  61.     OSErr aevtErr;
  62.  
  63.     aevtErr = noErr;
  64.     
  65.     if ( aevtErr == noErr )
  66.     {
  67.         OAPPHandlerUPP = NewAEEventHandlerProc(HandleOAPP);
  68.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, OAPPHandlerUPP, 0, false );
  69.     }
  70.  
  71.  
  72.     if ( aevtErr == noErr )
  73.     {
  74.         ODOCHandlerUPP = NewAEEventHandlerProc(HandleODOC);
  75.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, ODOCHandlerUPP, 0, false );
  76.     }
  77.  
  78.     if ( aevtErr == noErr )
  79.     {
  80.         PDOCHandlerUPP = NewAEEventHandlerProc(HandlePDOC);
  81.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, PDOCHandlerUPP, 0, false );
  82.     }
  83.  
  84.     if ( aevtErr == noErr )
  85.     {
  86.         QUITHandlerUPP = NewAEEventHandlerProc(HandleQuit);
  87.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, QUITHandlerUPP, 0, false );
  88.     }
  89.  
  90.     if ( aevtErr == noErr )
  91.         InstallOtherEvents ();
  92.  
  93.         
  94.     if ( aevtErr != noErr )
  95.         ;        // report an error if you are so included
  96. }
  97.  
  98.  
  99.  
  100. /*    
  101.     This routine is a utility routine for checking that all required 
  102.     parameters in the Apple event have been used.
  103. */
  104. #pragma segment Main
  105. OSErr GotRequiredParams ( AppleEvent *theAppleEvent )
  106. {
  107.     DescType    typeCode;
  108.     Size        actualSize;
  109.     OSErr        retErr, err;
  110.  
  111.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr,
  112.                     typeWildCard, &typeCode, NULL, 0, &actualSize );
  113.     
  114.     if ( err == errAEDescNotFound )    // we got all the required params: all is ok
  115.         retErr = noErr;
  116.     else if ( err == noErr )
  117.         retErr = errAEEventNotHandled;
  118.     else 
  119.         retErr = err;
  120.     
  121.     return retErr;
  122. }
  123.  
  124. /*
  125.     This is another routine useful for showing debugging info.
  126.     It calls the ErrorAlert routine from DSUtils to put up the 
  127.     error message.
  128.  
  129. */
  130. void FailErr(OSErr err)
  131. {
  132.     if (err != noErr)
  133.         ErrorAlert(kErrStringID, kAEVTErr, err);
  134. }
  135.  
  136. /*    
  137.     This routine is the handler for the oapp (Open Application) event.
  138.     
  139.     It first checks the number of parameters to make sure we got them all 
  140.     (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  141.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  142. */
  143. #pragma segment Main
  144. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon )
  145. {
  146. #pragma unused ( handlerRefcon )
  147.     OSErr err;
  148.  
  149.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  150.  
  151.     // let's show the user the splash screen
  152.     if ( gSplashScreen != NULL )
  153.         ShowWindow ( gSplashScreen );
  154.  
  155.     OpenApp ();        // pass it on to the app specific routine
  156.  
  157.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  158.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Opening", 7 ));
  159.     
  160.     return err;
  161. }
  162.  
  163.  
  164. /*    
  165.     This routine is the handler for the quit (Quit Application) event.
  166.     
  167.     It first checks the number of parameters to make sure we got them all 
  168.     (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  169.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  170. */
  171.  
  172. #pragma segment Main
  173. pascal OSErr HandleQuit ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon )
  174. {
  175. #pragma unused ( handlerRefcon )
  176.     OSErr err;
  177.     
  178.     FailErr( err = GotRequiredParams ( theAppleEvent ));
  179.  
  180.     QuitApp ();        // pass it on to the app specific routine
  181.  
  182.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  183.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Qutting", 7 ));
  184.     
  185.     return err;
  186. }
  187.  
  188.  
  189. /*    
  190.     This routine is the low level processing routine for both the 
  191.     odoc (Open Document) and pdoc (Print Document) events.
  192.     
  193.     This routine is the key one, since this is how we get the list of
  194.     files/folders/disks to process.  The first thing to do is the get the
  195.     list of files, and then make sure that's all the parameters (should be!).
  196.     We then send call the PreflightDocs routine (from DSUserProcs), process
  197.     each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  198.     call PostflightDocs (you know where) and setting a return value.
  199. */
  200. #pragma segment Main
  201. pascal OSErr _HandleDocs ( AppleEvent *theAppleEvent, AppleEvent *reply, Boolean opening )
  202. {
  203. #pragma unused ( reply )
  204.     OSErr        err;
  205.     FSSpec        myFSS;
  206.     AEDescList    docList;
  207.     long        index, itemsInList;
  208.     Size        actualSize;
  209.     AEKeyword    keywd;
  210.     DescType    typeCode;
  211.     Handle        userDataHandle;
  212.     
  213.  
  214.     FailErr(err = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList ));
  215.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  216.  
  217.     /*    How many items do we have?. */
  218.     /* NOTE: Moved here in DS 2.0 due to requests for this info in preflighter */
  219.     FailErr(err = AECountItems ( &docList, &itemsInList ));
  220.     
  221.     if (PreFlightDocs (opening, itemsInList, &userDataHandle))    // let the app do any preflighting it might need
  222.     {
  223.         for ( index = 1; index <= itemsInList; index++ )
  224.         {
  225.             FailErr(err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &typeCode,
  226.                     (Ptr) &myFSS, sizeof ( myFSS ), &actualSize ));
  227.     
  228.             OpenDoc( &myFSS, opening, userDataHandle );            // call the userProc
  229.         }
  230.         PostFlightDocs (opening, itemsInList, userDataHandle);    // cleanup time
  231.     }
  232.     else
  233.         err = errAEEventNotHandled;    // tells AEM that we didn't handle it!
  234.         
  235.     FailErr(AEDisposeDesc ( &docList ));
  236.  
  237.     return err;
  238. }
  239.  
  240. /*
  241.     This routine is the handler for the odoc (Open Document) event.
  242.     
  243.     The odoc event simply calls the common _HandleDocs routines, which will
  244.     do the dirty work of parsing the AEVT & calling the userProcs.
  245. */
  246. #pragma segment Main
  247. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon )
  248. {
  249. #pragma unused ( handlerRefcon )
  250.     
  251.     return (_HandleDocs(theAppleEvent, reply, true));    // call the low level routine
  252. }
  253.  
  254. /*
  255.     This routine is the handler for the pdoc (Print Document) event.
  256.     
  257.     The pdoc event like the odoc simply calls the common _HandleDocs routines
  258. */
  259. #pragma segment Main
  260. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon )
  261. {
  262. #pragma unused ( handlerRefcon )
  263.     
  264.     return (_HandleDocs(theAppleEvent, reply, false));    // call the low level routine
  265. }
  266.  
  267. /*    
  268.     This is the routine called by the main event loop, when a high level
  269.     event is found.  Since we only deal with Apple events, and not other
  270.     high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  271. */
  272. #pragma segment Main
  273. pascal void DoHighLevelEvent ( EventRecord *event )
  274. {
  275.     FailErr ( AEProcessAppleEvent ( event ) );
  276. }
  277.